package com.stedu.mall.userservice.controller;


import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.common.utils.JwtUtil;
import com.stedu.mall.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/captcha")
    public RespBean captcha()
    {
        //创建验证码的对象
        LineCaptcha captcha = new LineCaptcha(120, 38,4, 10);
        //生成唯一的key
        String key = IdUtil.getSnowflakeNextIdStr();
        //将key和验证码文本保存在Redis中-超时时间60秒
        redisUtil.set(key, captcha.getCode(),60);
        //将验证码转换成Base64编码的形式
        String imageBase64Data = captcha.getImageBase64Data();
        //封装到map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("imageBase64Data", imageBase64Data);
        //将key和Base64图片返回
        return RespBean.ok("验证码获取成功", map);
    }
    @PostMapping("/login")
    public RespBean login(String username, String password, String key, String captchaInput) throws SteduException {
        //判断验证码是否正确 -从Redis中获取验证码进行判断
        String captcha = (String) redisUtil.get(key);
        if(captcha ==null ||!captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误");
        }
        //登录
        User user = userService.login(username, password);
        //生成token
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("role", "user");
        String jwt = JwtUtil.generateJwt(map);
        return RespBean.ok("登录成功", jwt);
    }
    //获取登录信息
    @GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id
        Integer id = (Integer) map.get("id");
        User user = userService.selectById(id);
        //返回登录信息
        return RespBean.ok("登录信息获取成功", user);
    }
    @PostMapping("/reg")
    public RespBean reg(String username, String password,String key, String captchaInput) throws SteduException {
        //判断验证码是否正确 -从Redis中获取验证码进行判断
        String captcha = (String) redisUtil.get(key);
        if(captcha ==null ||!captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误");
        }
        //注册
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.reg(user);
        return RespBean.ok("注册成功");
    }

    @GetMapping("/selectByCondition")
    public RespBean selectByCondition(User condition, Integer pageNum, Integer pageSize) {
        PageInfo<User> userPageInfo = userService.selectAllByCondition(condition, pageNum, pageSize);
        return RespBean.ok("", userPageInfo);
    }
    @GetMapping("/selectById/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) {
        User user = userService.selectById(id);
        return RespBean.ok("", user);
    }
    @PostMapping("/disable/{id}")
    public RespBean updateStatus(@PathVariable("id") Integer id) throws SteduException {
        User newUser = new User();
        newUser.setId(id);
        newUser.setStatus(2);
        userService.update(newUser);
        return RespBean.ok("禁用账户成功");
    }
    //修改
    @PutMapping("/update")
    public RespBean update(@RequestBody User user, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        user.setId(userId);
        userService.update(user);
        return RespBean.ok("修改成功");
    }
    //修改密码
    @PutMapping("/updatePassword")
    public RespBean updatePassword(String oldPassword, String newPassword, String confirmPassword, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        userService.updatePassword(oldPassword, newPassword, confirmPassword, userId);
        return RespBean.ok("修改密码成功");
    }
    //修改支付密码
    @PutMapping("/updatePayPassword")
    public RespBean updatePayPassword(String loginPassword, String newPayPassword, String confirmPayPassword, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        userService.updatePayPassword(loginPassword, newPayPassword, confirmPayPassword, userId);
        return RespBean.ok("修改支付密码成功");
    }
}
